package com.yanqu.road.server.cross3server.cmd.activity.rankactivity;

import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.entity.rank.xrank.XChannelRank;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankListModel;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankMap;
import com.yanqu.road.entity.rank.xrank.XUserRank;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankListModel;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankMap;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross3.activity.ChannelRankActivityProto;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.cross3server.manager.Cross3XRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;

import java.util.List;

@Cmd(code = Cross3Protocol.C3_CHANNEL_RANK_GET_RANK_TOP, desc = "获取跨地区冲榜榜首")
public class Cross3ChannelRankActivityGetRankTopCmd extends CrossCommand {

    private int activityId;
    private int userConditionType;
    private int channelConditionType;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChannelRankActivityProto.CrossGetChannelRankTopReqMsg reqMsg = ChannelRankActivityProto.CrossGetChannelRankTopReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userConditionType = reqMsg.getUserConditionType();
        channelConditionType = reqMsg.getChannelConditionType();

        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChannelRankActivityProto.GetChannelRankTopRespMsg.Builder builder = ChannelRankActivityProto.GetChannelRankTopRespMsg.newBuilder();
        builder.setRet(0);

        // 个人榜榜首
        XUserRankMap userRankMap = Cross3XRankMgr.getUserRankMap();
        XUserRankListModel userRankListModel = userRankMap.getUserRankListModel(activityId, userConditionType);
        List<XUserRank> userRankList = userRankListModel.getRankList(Math.min(userRankListModel.getMaxRank(), 3));
        XRankProto.XUserRankListMsg.Builder userRankListMsg = XRankPb.parseXUserRankListMsg(activityId, userConditionType, userRankList, false);
        XRankPb.fillFlagId(userRankListMsg);
        builder.setUserTopList(userRankListMsg);
        // 个人榜排名
        builder.setMyUserRank(userRankListModel.getMyRank(userId));
        // 区域榜榜首
        XChannelRankMap channelRankMap = Cross3XRankMgr.getChannelRankMap();
        XChannelRankListModel channelRankListModel = channelRankMap.getChannelRankListModel(activityId, channelConditionType);
        List<XChannelRank> channelRankList = channelRankListModel.getRankList(3);
        XRankProto.XChannelRankListMsg.Builder channelRankListMsg = XRankPb.parseXChannelRankListMsg(activityId, channelConditionType, channelRankList);
        XRankPb.fillFlagId(channelRankListMsg);
        builder.setChannelTopList(channelRankListMsg);

        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CHANNEL_RANK_GET_RANK_TOP, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
